package com.ruibei.ui;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.ruibei.R;
import com.ruibei.utils.Utils;
import com.ruibei.view.ActionSheet;
import com.ruibei.view.ActionSheet.ActionSheetListener;
/**
 * 
 * @author wu
 *	历史过滤页面
 */
public class HistoryFilterActivity extends BaseActivity implements OnClickListener{

	@InjectView(R.id.history_filter_time) 
	RelativeLayout history_fillter_interval;
	@InjectView(R.id.history_filter_date) 
	RelativeLayout history_filter_date;
	@InjectView(R.id.history_filter_date_end) 
	RelativeLayout history_filter_date_end;
	@InjectView(R.id.history_filter_status)
	RelativeLayout history_filter_status;
	@InjectView(R.id.history_filter_classification) 
	RelativeLayout history_filter_classification;
	@InjectView(R.id.history_filter_order_by) 
	RelativeLayout history_filter_order_by;
	@InjectView(R.id.history_filter_interval)TextView interval_text;
	@InjectView(R.id.history_filter_classification_detail)TextView classification;
	@InjectView(R.id.history_filter_detail_from_date)TextView from_date;
	@InjectView(R.id.history_filter_detail_to_date)TextView to_date;
	@InjectView(R.id.history_filter_order_by_detail)TextView order_by;
	@InjectView(R.id.history_filter_status_detail)TextView status;
	@InjectView(R.id.history_query)Button searchBtn;
	
	
	
	@InjectView(R.id.history_query) Button history_query;
	private String[] sheetItems;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_history_filter);
		ButterKnife.inject(this);
		initListener();
	}
	

	private void initListener() {
		history_fillter_interval.setOnClickListener(this);
		history_filter_date.setOnClickListener(this);
		history_filter_date_end.setOnClickListener(this);
		history_filter_status.setOnClickListener(this);
		history_filter_classification.setOnClickListener(this);
		history_filter_order_by.setOnClickListener(this);
		searchBtn.setOnClickListener(this);
	}


	@Override
	public void onClick(View arg0) {
		int mID = arg0.getId();
		System.out.println("==========="+mID);
		if(mID==R.id.history_filter_time){
			setTheme(R.style.ActionSheetStyleIOS7);
			List<String> list = new ArrayList<String>();
			list.add("本周");
			list.add("本月");
			list.add("本季度");
			list.add("本年");
			showActionSheet(list, "interval");
		}
		if(mID==R.id.history_filter_date){
			Utils.getDateTime(from_date,HistoryFilterActivity.this);
		}
		if(mID==R.id.history_filter_date_end){
			Utils.getDateTime(to_date,HistoryFilterActivity.this);
		}
		if(mID==R.id.history_filter_status){
			setTheme(R.style.ActionSheetStyleIOS7);
			List<String> list = new ArrayList<String>();
			list.add("全部");
			list.add("状态1");
			list.add("状态2");
			list.add("状态3");
			showActionSheet(list, "status");
		}
		if(mID==R.id.history_filter_classification){
			setTheme(R.style.ActionSheetStyleIOS7);
			List<String> list = new ArrayList<String>();
			list.add("全部");
			list.add("分类1");
			list.add("分类2");
			list.add("分类3");
			showActionSheet(list, "classification");
		}
		if(mID==R.id.history_filter_order_by){
			setTheme(R.style.ActionSheetStyleIOS7);
			List<String> list = new ArrayList<String>();
			list.add("从新到旧");
			list.add("从旧到新");
			showActionSheet(list, "order");
		}
		if(mID==R.id.history_query){
			finish();
			overridePendingTransition(R.anim.fade_in,R.anim.activity_down);
		}
		
	}
	
	public void showActionSheet(List<String> list, String type) {
		sheetItems = (String[]) list.toArray(new String[list.size()]);
		if (type == "interval") {
			SheetIntervalClickListener sheetIntervalClickListener = new SheetIntervalClickListener();
			ActionSheet.createBuilder(this, getSupportFragmentManager())
					.setCancelButtonTitle("Cancel")
					.setOtherButtonTitles(sheetItems)
					.setCancelableOnTouchOutside(true)
					.setListener(sheetIntervalClickListener).show();
		} else if (type == "status") {
			SheetStatusClickListener sheetStatusClickListener = new SheetStatusClickListener();
			ActionSheet.createBuilder(this, getSupportFragmentManager())
					.setCancelButtonTitle("Cancel")
					.setOtherButtonTitles(sheetItems)
					.setCancelableOnTouchOutside(true)
					.setListener(sheetStatusClickListener).show();
		} else if (type == "classification") {
			SheetClassificationClickListener sheetClassificationClickListener = new SheetClassificationClickListener();
			ActionSheet.createBuilder(this, getSupportFragmentManager())
					.setCancelButtonTitle("Cancel")
					.setOtherButtonTitles(sheetItems)
					.setCancelableOnTouchOutside(true)
					.setListener(sheetClassificationClickListener).show();
		} else if (type == "order") {
			SheetOrderClickListener sheetOrderClickListener = new SheetOrderClickListener();
			ActionSheet.createBuilder(this, getSupportFragmentManager())
					.setCancelButtonTitle("Cancel")
					.setOtherButtonTitles(sheetItems)
					.setCancelableOnTouchOutside(true)
					.setListener(sheetOrderClickListener).show();
		} 

	}
	
	@SuppressWarnings("unused")
	private class SheetIntervalClickListener implements ActionSheetListener {
		@Override
		public void onOtherButtonClick(ActionSheet actionSheet, int index) {
			interval_text.setText(sheetItems[index]);
		}

		@Override
		public void onDismiss(ActionSheet actionSheet, boolean isCancel) {
		}
	}
	@SuppressWarnings("unused")
	private class SheetStatusClickListener implements ActionSheetListener {
		@Override
		public void onOtherButtonClick(ActionSheet actionSheet, int index) {
			status.setText(sheetItems[index]);
		}

		@Override
		public void onDismiss(ActionSheet actionSheet, boolean isCancel) {
		}
	}
	
	@SuppressWarnings("unused")
	private class SheetClassificationClickListener implements ActionSheetListener {
		@Override
		public void onOtherButtonClick(ActionSheet actionSheet, int index) {
			classification.setText(sheetItems[index]);
		}

		@Override
		public void onDismiss(ActionSheet actionSheet, boolean isCancel) {
		}
	}
	@SuppressWarnings("unused")
	private class SheetOrderClickListener implements ActionSheetListener {
		@Override
		public void onOtherButtonClick(ActionSheet actionSheet, int index) {
			order_by.setText(sheetItems[index]);
		}

		@Override
		public void onDismiss(ActionSheet actionSheet, boolean isCancel) {
		}
	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			overridePendingTransition(R.anim.fade_in,R.anim.activity_down);
		}
		return super.onKeyDown(keyCode, event);
	}
}
